<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/welcome' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片视图 -->
    <el-card>
        <el-row>
          <el-col>
             <el-input placeholder="请输入内容" v-model="queryinfo.query" clearable @input="searchGoods($event)" class="input-with-select">
                <el-button slot="append" icon="el-icon-search" @click="searchGoods()"></el-button>
             </el-input>
             <el-Button type="primary" @click="goAddGoods()">添加商品</el-Button>
          </el-col>
        </el-row>
        <!-- 数据列表 -->
        <el-table border stripe :data="goods_list">
          <!-- 索引列 -->
          <el-table-column type="index" label="#"></el-table-column>
          <el-table-column  label="商品名称" prop="goods_name"></el-table-column>
          <el-table-column  label="商品价格" prop="goods_price"></el-table-column>
          <el-table-column  label="商品重量" prop="goods_weight"></el-table-column>
          <el-table-column  label="创建时间" prop="add_time" >
            <template slot-scope="scope">
              {{ scope.row.add_time | dateformate }}
            </template>
          </el-table-column>
          <el-table-column  label="操作">
            <template slot-scope="scope">
                <el-Button type="primary" size="mini" @click="showCopyReaderDialog(scope.row.goods_id)">编辑</el-Button>
                <el-Button type="danger" size="mini" @click="deleteGood(scope.row)">删除</el-Button>
            </template>
          </el-table-column>
        </el-table>

        <!-- 分页 -->
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="queryinfo.pagenum"
          :page-sizes="[3, 5, 8, 10]"
          :page-size="1"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total">
        </el-pagination>
    </el-card>

    <!-- 编辑用户对话框 -->
    <el-dialog
      title="编辑用户"
      :visible.sync="setCopyReaderDialogVisible"
      width="50%">
      <el-form ref="CopyReaderFormRef" :model="CopyReaderForm" label-width="80px">
        <el-form-item label="商品名称">
          <el-input v-model="CopyReaderForm.goods_name"></el-input>
        </el-form-item>
        <el-form-item label="商品价格">
          <el-input v-model="CopyReaderForm.goods_price"></el-input>
        </el-form-item>
        <el-form-item label="商品数量">
          <el-input v-model="CopyReaderForm.goods_number"></el-input>
        </el-form-item>
        <el-form-item label="商品重量">
          <el-input v-model="CopyReaderForm.goods_weight"></el-input>
        </el-form-item>
        <el-form-item label="商品介绍">
          <el-input v-model="CopyReaderForm.goods_introduce"></el-input>
        </el-form-item>
      </el-form>
        <span slot="footer">
          <el-button @click="setCopyReaderDialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="CopyReader()">确 定</el-button>
        </span>
    </el-dialog>

  </div>
</template>

<script>
export default {
  data() {
    return {
      //   列表数据
      queryinfo: {
        query: '',
        pagenum: 1,
        pagesize: 3
      },
      //   所有的商品列表数据
      goods_list: [],
      //  总数据条数
      total: 0,
      //   编辑用户对话框是否显示
      setCopyReaderDialogVisible: false,
      //  编辑提交用户数据的对象
      CopyReaderForm: {}
    }
  },
  created() {
    this.getGoods_list()
  },
  methods: {
    // 获取商品列表
    getGoods_list() {
      this.$http.get('goods', {
        params: this.queryinfo
      }).then(res => {
        if (res.data.meta.status !== 200) return this.$message.error('获取商品列表失败')
        this.goods_list = res.data.data.goods
        this.total = res.data.data.total
      })
    },
    // 当分页功能页显示数据条数改变时
    handleSizeChange(newSize) {
      this.queryinfo.pagesize = newSize
      this.getGoods_list()
    },
    // 当分页功能页显示页数改变时
    handleCurrentChange(newNum) {
      this.queryinfo.pagenum = newNum
      this.getGoods_list()
    },
    // 显示编辑商品对话框 并传递商品ID值
    showCopyReaderDialog(id) {
      this.setCopyReaderDialogVisible = true
      this.$http.get(`goods/${id}`).then(res => {
        console.log(res.data)
        if (res.data.meta.status !== 200) return this.$message.error('获取商品信息失败')
        this.CopyReaderForm = res.data.data
      })
    },
    // 点击确定按钮 提交表单数据对象 完成编辑功能
    CopyReader() {
      this.$http.put(`goods/${this.CopyReaderForm.cat_id}`, {
        goods_name: this.CopyReaderForm.goods_name,
        goods_price: this.CopyReaderForm.goods_price,
        goods_number: this.CopyReaderForm.goods_number,
        goods_weight: this.CopyReaderForm.goods_weight,
        goods_introduce: this.CopyReaderForm.goods_introduce,
        goods_cat: '1,2,3',
        pics: '',
        attrs: ''
      }).then(res => {
        console.log(res.data)
        if (res.data.meta.status !== 200) return this.$message.error('更新商品信息失败')
        this.$message.success('更新商品信息成功')
        this.setCopyReaderDialogVisible = false
        this.getGoods_list()
        this.$refs.CopyReaderFormRef.resetFields()
      })
    },
    // 删除商品
    async deleteGood(row) {
      const confirmResult = await this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(err => err)

      console.log(confirmResult)
      if (confirmResult !== 'confirm') {
        return this.$message.info('已取消删除')
      }

      // 删除用户
      this.$http.delete(`goods/${row.goods_id}`).then(res => {
        console.log(res)
        const { data } = res
        if (data.meta.status !== 200) return this.$message.error('删除商品失败')
        this.$message.success('删除商品成功')
        this.getGoods_list()
      })
    },
    // 搜索商品
    searchGoods(e) {
      this.getGoods_list()
    },
    // 去往添加商品组件
    goAddGoods() {
      // 路由重定向
      this.$router.push({ path: '/goods/add' })
    }
  }
}
</script>

<style lang="less" scoped>
  .input-with-select {
      width: 200px;
      margin-right: 10px;
  }
</style>
